package com.maszy.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.maszy.bean.Car;
import com.maszy.service.CarService;

public class CarServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	CarService service = new CarService();
	String view = new String(); // 页面路径

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		CarServlet servlet = new CarServlet();
		RequestDispatcher rd = null;
		String method = request.getParameter("method");
		if ("queryAll".equals(method)) {
			// 查询出全部数据
			servlet.queryAll(request, response);
			view = "/background/car/car.jsp";
		} else if ("insert".equals(method)) {
			// 添加新记录
			servlet.add(request, response);
			servlet.queryAll(request, response);
			view = "/background/car/car.jsp";
		} else if ("update".equals(method)) {
			// 修改记录
			servlet.update(request, response);
			servlet.queryAll(request, response);
		} else if ("delete".equals(method)) {
			// 删除记录
			servlet.delete(request, response);
			servlet.queryAll(request, response);
		} else if ("updateForward".equals(method)) {
			// 进入修改页面
			servlet.queryForOne(request, response);
		} else if ("insertForward".equals(method)) {
			// 进入修改页面
			view = "/background/car/add_car.jsp";
		} else if ("queryReport".equals(method)) {
			// 报表页面
			servlet.queryReport(request, response);
			view = "/background/car/car_report.jsp";
		} else if ("insertCarMilage".equals(method)) {
			insertCarMilage(request, response);
//			servlet.queryReport(request, response);
			view = "/background/car/carMain.jsp";
		} else if ("insertCarPay".equals(method)) {
			insertCarPay(request, response);
//			servlet.queryReport(request, response);
			view = "/background/car/carMain.jsp";
		}else if ("queryAllForward".equals(method)) {			
			view = "/background/car/carMain.jsp";
		}else if("query_carmilage".equals(method)){
			
		}

		try {
			rd = this.getServletContext().getRequestDispatcher(view);
			rd.forward(request, response);

		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void queryReport(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String bTime=request.getParameter("time1");
		String eTime=request.getParameter("time2");
		
		List carList=service.queryAllCar();
		List reportList=new ArrayList();
		Map m=new HashMap();
		for ( int i = 0; i<carList.size();i++){
			m=service.queryCarReport(((Car)carList.get(i)).getCar_Id(),bTime,eTime);
			m.put("car_Id", ((Car)carList.get(i)).getCar_Id());
			reportList.add(m);
		
		}
		request.setAttribute("bTime", bTime);
		request.setAttribute("eTime", eTime);
		request.setAttribute("list", reportList);
	}

	private void queryForOne(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
	}

	private void insertCarMilage(HttpServletRequest request,
			HttpServletResponse response) {
		Car obj = new Car();
		try {
			obj.setCar_Id(Integer.parseInt(request.getParameter("Car_Id")));
			obj.setCarMilage_Milage(Double.parseDouble(request
					.getParameter("CarMilage_Milage")));
			obj.setCarMilage_Oil(Double.parseDouble(request
					.getParameter("CarMilage_Oil")));
			obj.setCarMilage_Date(request.getParameter("time"));
			obj.setCarMilage_Remark(request.getParameter("Car_Remark"));
			service.addCarMilage(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void insertCarPay(HttpServletRequest request,
			HttpServletResponse response) {
		Car obj = new Car();
		try {
			obj.setCar_Id(Integer.parseInt(request.getParameter("Car_Id")));
			obj.setCarPay_Type(request.getParameter("CarPay_Type"));
			if("1".equals(obj.getCarPay_Type())){
				obj.setCarPay_OilCount(Double.parseDouble(request.getParameter("CarPay_OilCount")));
			}else{
				obj.setCarPay_OilCount(0d);
			}
			obj.setCarPay_Content(request.getParameter("CarPay_Content"));
			obj.setCarPay_Pay(Double.parseDouble(request
					.getParameter("CarPay_Pay")));
			obj.setCarPay_Date(request.getParameter("time"));
			obj.setCarPay_Remark(request.getParameter("CarPay_Remark"));
			service.addCarPay(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub

	}

	private void update(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
	}

	private void queryAll(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		List list = service.queryAllCar();
		request.setAttribute("list", list);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		Car obj = new Car();
		obj.setCar_NO(request.getParameter("Car_NO"));
		obj.setCar_Type(request.getParameter("Car_Type"));
		obj.setCar_Driver(request.getParameter("Car_Driver"));
		obj.setCar_Status(request.getParameter("Car_Status"));
		obj.setCar_Remark(request.getParameter("Car_Remark"));
		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(Calendar.getInstance().getTime());
		obj.setCar_Date(time);
		obj.setCarMilage_Date(time);
		try {
			obj.setCarMilage_Milage(Double.parseDouble(request
					.getParameter("CarMilage_Milage")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			obj.setCarMilage_Oil(Double.parseDouble(request
					.getParameter("CarMilage_Oil")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		service.addCar(obj);
		service.addCarMilage(obj);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		doGet(request, response);
	}
}